#include "EpilAssignStatementParser.h"
#include "EpilPrimaryExpressionParser.h"
#include "EpilExpressionListParser.h"
#include "EpilParserException.h"
#include <algorithm>

using namespace std;


EpilAssignStatementParser::EpilAssignStatementParser( EpilTokenStream & tokens )
: EpilSyntaxParserBase( tokens )
{
}


void EpilAssignStatementParser::Parse()
{
	switch( this->GetTokens().GetCurrentToken().type )
	{
	case ETT_OpComma:
		{
			// assignment -> `,' primaryexp assignment
			this->GetTokens().MoveToNext();

			EpilPrimaryExpressionParser primaryExprParser( this->GetTokens() );
			primaryExprParser.Parse();
			EpilExpressionCodeBase * headExpr = primaryExprParser.GetExpression();
			m_assignees.push_back( headExpr );

			EpilAssignStatementParser assignStatParser( this->GetTokens() );
			assignStatParser.Parse();
			EpilExpressionCodeList & assignees = assignStatParser.GetAssignees();
			copy( assignees.begin(), assignees.end(), back_inserter( m_assignees ) );
			EpilExpressionCodeList & assigners = assignStatParser.GetAssigners();
			m_assigners = assigners;
		}
		break;
	case ETT_OpAssign:
		{
			// assignment -> `=' explist1
			this->GetTokens().MoveToNext();

			EpilExpressionListParser exprListParser( this->GetTokens() );
			exprListParser.Parse();
			m_assigners = exprListParser.GetExpressions();
		}
		break;
	default:
		break;
	}
}